package com.kennedy.atoupdate.web.controller;

import com.github.pagehelper.PageInfo;
import com.kennedy.atoupdate.beans.GroupInfo;
import com.kennedy.atoupdate.beans.VersionInfo;
import com.kennedy.atoupdate.beans.common.ResultMsg;
import com.kennedy.atoupdate.service.GroupInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by tao.zeng on 2017/8/17.
 */
@Controller
@RequestMapping("/group")
public class GroupInfoController {
    @Autowired
    private GroupInfoService groupInfoService;

    @GetMapping("/toList")
    public String toLogin() {
        return "group/list";
    }

    @GetMapping("/toAdd")
    public String toAdd() {
        return "group/add";
    }

    @GetMapping("/getListByPage")
    @ResponseBody
    public PageInfo<GroupInfo> getListByPage(GroupInfo group) {
        List<GroupInfo> groups = groupInfoService.getListByPage(group);
        return new PageInfo<GroupInfo>(groups);
    }

    @DeleteMapping("/delete/{id}")
    @ResponseBody
    public ResultMsg delete(@PathVariable("id") Long id) {
        try {
            groupInfoService.delete(id);
        } catch (Exception e) {
            return ResultMsg.error();
        }
        return ResultMsg.success();
    }

    @PostMapping("/add")
    @ResponseBody
    public ResultMsg add(GroupInfo groupInfo) {
        try {
            groupInfoService.insert(groupInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultMsg.success();
    }
}
